package basic;

import java.util.HashMap;

/**
 * @date 2023/6/2 18:19
 * @version：1.0
 */
public class MapTest {

    public static void main(String[] args) {
        // 创建一个 HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往HashMap中添加映射关系
        prices.put("Shoes", 180);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);

        // Shoes2中的映射关系不存在
        int shoePrice2 = prices.computeIfAbsent("Shoes2", (key) -> 280);
        prices.putIfAbsent("Shoes2", 281);
        System.out.println("Price of Shoes2: " + shoePrice2);
        // Shoes并没有计算新值
        int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280);
        System.out.println("Price of Shoes: " + shoePrice);

        // 输出更新后的 HashMap
        System.out.println("Updated HashMap: " + prices);
    }
}
